package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.common.util.StrUtils;
import com.geyue.core.model.*;
import com.geyue.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by Lyon on 2017/4/20.
 */
@RestController
@RequestMapping("/api/live")
public class LiveController {

    @Autowired
    private LiveService liveService;

    @Autowired
    private FocusService focusService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private GiftService giftService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private Live_StudentService live_studentService;

    /**
     * 查询所有直播
     * @return
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public Object allLive(){

        return liveService.queryAll();
    }

    /**
     * 直播分页
     * @return
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Res list(Integer pageSize, Integer pageNum){

        return Res.success("",liveService.getPage(pageNum,pageSize,new Live()));
    }

    /**
     * 点击直播，进入直播页面
     * @return
     */
    @RequestMapping(value = "/getLive",method = RequestMethod.GET)
    @ResponseBody
    public Live getLive(Integer liveId,Integer stuId,HttpSession session){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);
        if(student != null){

            //点击观看，观看次数添加1
            liveService.addTotalById(liveId);

            Live_Student live_Student=live_studentService.getLive_Student(liveId,student.getId());

            if(live_Student == null){

                Live_Student live_student=new Live_Student();
                live_student.setLiveVedioId(liveId);
                live_student.setStudentId(student.getId());
                live_student.setType(1);
                live_student.setAddTime(new Date());
                live_studentService.save(live_student);
            }

            return liveService.getLive(liveId);
        }
        return null;

    }

    /**
     * 获取正在看直播的学生的头像
     */
    @RequestMapping(value = "/getStudent",method = RequestMethod.GET)
    @ResponseBody
    public Object getStudent(Integer liveId){

        return liveService.getStudent(liveId,1);
    }

    /**
     * 点击关注
     */
    @RequestMapping(value = "/addFocus",method = RequestMethod.GET)
    @ResponseBody
    public String addFocus(Integer liveId,Integer stuId,Integer type,HttpSession session){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);
        if(student !=null){
            if(type ==0){

                focusService.deleteFocus(liveId,stuId,1);

                return "已取消关注！";

            }else {
                if(liveId != 0){

                    Focus focus=new Focus();
                    focus.setFocusId(liveId);
                    focus.setStudentId(stuId);
                    focus.setType(1);
                    focus.setAddTime(new Date());
                    focusService.save(focus);

                    return "关注成功！";
                }else{
                    return "关注失败！";
                }
            }
        }
        return "关注失败！";
    }

    /**
     * 点击教师头像
     */
    @RequestMapping(value = "/getTeacher",method = RequestMethod.GET)
    @ResponseBody
    public Teacher getTeacher(int teaId){

        return teacherService.getOneByKey(teaId);
    }

    /**
     * 添加评论
     * @param
     * @return
     */
    @RequestMapping(value = "/addComment",method = RequestMethod.GET)
    @ResponseBody
    public String addComment(Integer liveId,Integer stuId,HttpSession session){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);
        if(student !=null){

            Comment comment=new Comment();
            comment.setLiveVedioId(liveId);
            comment.setStudentId(student.getId());
            comment.setType(1);//1表示直播
            comment.setAddTime(new Date());
            commentService.save(comment);

            return "评论成功！";
        }

        return "评论失败！";
    }

    /**
     * 根据直播ID获取评论
     * @return
     */
    @RequestMapping(value = "/getCommentById",method = RequestMethod.GET)
    @ResponseBody
    public Object getCommentById(Integer liveId){

        return commentService.all(liveId);

    }

    /**
     * 礼物列表
     * @return
     */
    @RequestMapping(value = "/allGift",method = RequestMethod.GET)
    @ResponseBody
    public Object allGift(){

        return giftService.queryAll();
    }

    /**
     * 礼物购买
     * @param
     * @return
     */
    @RequestMapping(value = "/getGift",method = RequestMethod.GET)
    @ResponseBody
    public String getGift(Integer giftId,Integer stuId,HttpSession session){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);
        if(student !=null){

            int giftMoney=giftService.getOneByKey(giftId).getMoney();
            int stuMoney=studentService.getOneByKey(student.getId()).getMoney();

            if(giftMoney > stuMoney ){

                return "您的学习币不足，请及时充值！";

            }else{

                studentService.updateStudentById(student.getId(),giftService.getOneByKey(giftId).getMoney());
                return "购买成功！";
            }
        }else{

            return "赠送失败！";
        }


    }

    /**
     * 直播页面的查找
     * @return
     */
    @RequestMapping(value = "/searchLive",method = RequestMethod.GET)
    @ResponseBody
    public Object searchLive(String  keyword){

        return liveService.searchLive(keyword);
    }

    /**
     * 退出直播时，观看人数减少1
     * @return
     */
    @RequestMapping(value = "/deleteLiveTotal",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteVedioTotal(HttpSession session, Integer liveId,Integer stuId){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);

        live_studentService.deleteLiveStudent(liveId,student.getId(),1);

        return liveService.decreaseTotalById(liveId);
    }



}
